package com.admin.controller;


import com.admin.service.DatabaseService;
import com.admin.util.DatabaseUtil;
import com.admin.util.LogUtil;
import com.admin.util.Result;
import com.data.entity.Database;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 数据库备份
 */
@RestController
@RequestMapping("/database")
@Api(tags = "数据库备份")
public class DatabaseController {
    private Logger logger = LogUtil.getBussinessLogger();

    @Resource
    private DatabaseService databaseService;



    /**
     * 数据备份列表
     * @param limit 页码
     * @param page  页面长度
     * @return
     */
    @GetMapping("/listPage")
    @ApiOperation("数据备份列表")
    public Result<Database> list(Integer limit, Integer page) {
        Result result = new Result();
        try {
            List<Database> databaseList = databaseService.listPage(page, limit);
            result.setCode(Result.Code.SUCCESS);
            result.setData(databaseList);
            result.setCount(databaseService.count());
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }


    /**
     * 数据库备份
     * @return
     */
    @GetMapping("/backup")
    @ApiOperation("数据库备份")
    public Result backup() {
        Result result = new Result();
        try {
            result = databaseService.backup();
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }
    /**
     * 删除列表信息
     * @param id 数据备份id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("数据备份：删除")
    public Result delete(Long id){
        Result result=new Result();
        try {
            Database database = databaseService.getInfo(id);
            File file = new File(database.getDirectory());
            if (file.exists()){//文件是否存在
                file.delete();//删除文件
            }
            databaseService.delete(id);
            result.setCode(Result.Code.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            result.setCode(Result.Code.SYSTEMERROR);
        }
        return result;
    }

    /*
     * @Author wangghua
     * @Description 数据备份：还原
     * @Date 9:27 2021/4/9
     * @Param [id]
     * @return com.admin.util.Result
     **/
    @GetMapping("/rollback")
    @ApiOperation("数据备份：还原")
    public Result rollback(Long id){
        Result result=new Result();
        try {
            result = databaseService.rollback(id);
        }catch (Exception e){
            logger.error("【还原数据库】失败：{}", e.getMessage());
            result.setMessage(Result.Code.SYSTEMERROR,"还原数据库失败");
        }
        return result;
    }

}
